package com.ruoyi.framework.wx.handle;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.api.WxMpMessageRouter;
import me.chanjar.weixin.mp.api.WxMpService;

@Configuration
public class MpMessageRouter {

	@Autowired
	private WxMpService wxMpService;
	
	@Autowired
	private SubscribeHandler subscribeHandler;
	
	@Autowired
	private UnSubscribeHandler unSubscribeHandler;

	@Bean
	public WxMpMessageRouter messageRouter(){
		// 创建消息路由
		final WxMpMessageRouter router = new WxMpMessageRouter(wxMpService);
		// 关注事件
		router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
		.event(WxConsts.EventType.SUBSCRIBE).handler(subscribeHandler).end();
		// 取关事件
		router.rule().async(false).msgType(WxConsts.XmlMsgType.EVENT)
		.event(WxConsts.EventType.UNSUBSCRIBE).handler(unSubscribeHandler).end();
		return router;
	}
	
}
